<?php defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * 后台角色管理
 */
class Role extends Admin_Controller {


	public function __construct()
	{
		parent::__construct();

	} 


	//列表页
	public function index()
	{
		$this->load->model('admin/role_model', 'search_role');

		//状态数组
        $data["status_arr"] = Role_model::$status_arr;

		//分页
        $data["pageCurrent"] = ForceIntFrom('pageCurrent');
        $data["pageSize"] = ForceIntFrom('pageSize');

        $data["pageCurrent"] = Iif($data["pageCurrent"], $data["pageCurrent"], 1);
        $data["pageSize"] = Iif($data["pageSize"], $data["pageSize"], 20);

		//排序
        $data["orderField"] = trim($this->input->post('orderField', true));
        $data["orderDirection"] = trim($this->input->post('orderDirection', true));

        $data["orderField"] = Iif($data["orderField"], $data["orderField"], 'id');
        $data["orderDirection"] = Iif($data["orderDirection"], $data["orderDirection"], 'asc');


		//搜索关键字等
        $data['keytype'] = trim($this->input->post('keytype', true));
        $data['keyword'] = trim($this->input->post('keyword', true));

		//状态
		$data['status'] = trim($this->input->post('status', true));

        if(!empty($data['keyword']) && !empty($data['keytype']))
		{
            $this->search_role->$data['keytype'] = $data['keyword'];
		}

        if(!empty($data['status']) && $data['status'] != "all")
		{
			$this->search_role->status = $data['status'];
		}

		//获取记录列表
		$this->search_role->_order_by = Iif($data['orderField'] != 'admin_counts', "adro.") . "{$data[orderField]} {$data[orderDirection]}";
        $this->search_role->_page_index = $data["pageCurrent"];
        $this->search_role->_page_count = $data["pageSize"];
		$this->search_role->search();

        $data["list"] = $this->search_role->_search_results;

		//获取总记录数
        $this->search_role->_is_search_counter = true;
        $this->search_role->search();

        $data["totalCount"] = $this->search_role->_search_num_rows;


		$this->display('role/index.html', $data);
	}


	//编辑角色
	public function edit($id = 0)
	{
		$this->add($id);
	}


	//添加或编辑角色
	public function add($id = 0)
	{

		$data['role'] = array('id' => '', 'status' => '', 'role_name' => '');
		$data['permissions'] = array();

		$id = intval($id); //角色id

		//读取所有的权限资源
		$this->load->model('admin/resource_model', 'search_resource');
		$this->search_resource->_order_by = 'adrc.id ASC';
		$this->search_resource->search();

		$data['admin_resources'] = $this->search_resource->_search_results;


		//有id时加载角色 -- 编辑, 不可以编辑id==1的超级管理
		if($id AND $id != '1')
		{
			$this->load->model('admin/role_model', 'search_role');
			
			if(!$this->search_role->retrieve($id))
			{
				$this->error('角色不存在！');
			}

			//赋值
			$data['role'] = array(
				'id' => $this->search_role->id,
				'status' => $this->search_role->status,
				'role_name' => $this->search_role->role_name,
			);

			$data['permissions'] = unserialize($this->search_role->permissions); //角色权限数组
		}


		$this->display('role/edit.html', $data);
	}


	//保存角色
	public function save()
	{
		//初始化权限数组
		$permissions = array();

		$id = intval($_POST['id']); //角色id
		$status = intval($_POST['status']); //状态
		$role_name = trim($this->input->post('role_name', true)); //角色名称
		$parent_resources = $this->input->post('parent_resources', true); //数组

		if(empty($role_name))
		{
			$this->error('未填写角色名称！');
		}

		if(empty($parent_resources) OR !is_array($parent_resources))
		{
			$this->error('未选择任何权限！');
		}

		foreach($parent_resources AS $resource)
		{
			$resource_arr = explode('||', $resource);

			$resource_id = $resource_arr[0];
			$resource_file_path = strtolower($resource_arr[1]); //小写处理

			if(empty($resource_id) OR empty($resource_file_path))
			{
				continue;
			}

			$son_functions = $this->input->post("son_resources_" . $resource_id, true); //获取每个权限资源已选中的权限(function动作)

			if(empty($son_functions))
			{
				continue;
			}

			$permissions[$resource_file_path] = $son_functions;
		}


		if(empty($permissions))
		{
			$this->error('未选择任何权限！');
		}

		$this->load->model('admin/role_model');

		$this->role_model->role_name = $role_name;
		$this->role_model->permissions = serialize($permissions);

		if($id) { //更新数据

			//不可以编辑id==1的超级管理
			if($id == '1')
			{
				$this->error('角色不存在！');
			}

			$this->role_model->status = $status;
			$this->role_model->_update_where = array('id' => $id);

			if($this->role_model->update())
			{
				$this->success('角色 -> 更新成功！'); //编辑时不更新tab
			}

		}else{ //插入记录
			$this->role_model->status = Role_model::STATUS_SUCCESS;

			if($this->role_model->insert())
			{
				$this->success('角色 -> 添加成功！', 'admin-role-list'); //admin-role-list指需要更新的tabId
			}
		}

		$this->error();
	}


	//删除角色
	public function delete($id = 0)
	{
		$id = intval($id); //角色id

		//超级管理不允许删除
		if(empty($id) OR $id == '1')
		{
			$this->error('未选择需要删除的角色！');
		}

		//不允许删除存在用户的角色
		$this->load->model('admin/admin_model', 'search_admin');

		if($this->search_admin->check_exist_by_role_id($id))
		{
			$this->error('存在用户的角色无法删除！');
		}

		//删除		
		$this->load->model('admin/role_model');

		$this->role_model->_delete_where = array('id' => $id);

		if($this->role_model->delete())
		{
			$this->success('角色 -> 删除成功！', 'admin-role-list', false); //false表示: 不关闭当前tab或dialog
		}

		$this->error();
	}

}
